package si.unimb.engine;

import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.Random;

public class Lik implements KeyListener
{
	
	private int pos_x;						// X pozicija
	private int pos_y; 						// Y pozicija
	private int x_speed;					// X hitrost
	private int y_speed;					// Y hitrost
	private int height;						// Visina
	private int width;						// Sirina
	private int gamePanelWidth;     		// Dolzina appleta
	private int gamePanelHeight;    		// Visina appleta
	private boolean dead;					// Mrtva zoga
	private boolean player;					// Player1 ali Player2 <- definicja za kontrole
	private ArrayList<Rectangle> tockeP;	// Bounding box crte
	private ArrayList<Color> tockeB;
	private Random randColor;

	// Barva lika
	Color color;

	// Konstruktor
	public Lik (int x, int y, int h, int w, boolean p, int GPW, int GPH, Color color)
	{
		// inicializacija lika

		pos_x = x;
		pos_y = y;
		
		height = h;
		width = w;
		
		tockeP = new ArrayList<Rectangle>();
		tockeB = new ArrayList<Color>();
		
		if(!p){
			x_speed = w;
			y_speed = 0;
		}else{
			x_speed = -w;
			y_speed = 0;
		}
			
		dead = false;
		player = p;
		
		gamePanelWidth = GPW;
		gamePanelHeight = GPH;

		//randColor = new Random();
		this.color = color;

	}
	
	// Metoda za pridobitev trenutne x pozicije
	public int getX(){
		return pos_x;
	}
	
	// Metoda za pridobitev trenutne y pozicije
	public int getY(){
		return pos_y;
	}

	// Metoda za premikanje lika
	public boolean move() {
		if (!isOut() && !dead) {
			pos_x += x_speed;
			pos_y += y_speed;

			if (!isCollidingAt(pos_x, pos_y)) {
				tockeP.add(new Rectangle(pos_x, pos_y, width, height));
				tockeB.add(color);
				return true;
			} else {
				dead = true;
				return false;
			}
		} else {
			return false;
		}
	}

	// Metoda za ugotavljanje iterakcije z crto lika
	public boolean isCollidingAt(int x, int y)
	{
		if(tockeP.contains(new Rectangle(x, y, width, height)))
			return true;
		else
			return false;
	}
	
	public void setDead(boolean dead){
		this.dead = dead;
	}

	//Metoda za ugotavljanje roba appleta
	private boolean isOut ()
	{
		if (pos_x > gamePanelWidth - width/2) { 
			x_speed = -width;
			dead = true;
			return true;
		}  
		else if (pos_x < width/2) { 
			x_speed = +width;
			dead = true;
			return true;
		}
		else if (pos_y > gamePanelHeight - height/2) {
			y_speed = -height;
			dead = true;
			return true;
		}
		else if (pos_y < height/2) {
			y_speed = +height;
			dead = true;
			return true;
		}
		
		return false;
	}

	// Metoda za izris lika
	public void DrawBall (Graphics g)
	{	
		for(int i = 0; i < tockeP.size(); i++){
			if(!dead)
				g.setColor(tockeB.get(i));
			else
				g.setColor(Color.RED);
			g.fillRect(tockeP.get(i).x, tockeP.get(i).y, tockeP.get(i).width, tockeP.get(i).height);
		}
		if(!dead) 
			g.setColor (color);
		else
			g.setColor(Color.RED);
		g.fillRect(pos_x - width/2, pos_y - height/2, width*2, height*2);
	}
	
	public Color randomColor()
    {
        return(new Color(randColor.nextInt(256), 
                         randColor.nextInt(256),
                         randColor.nextInt(256)));
    }

	//Metoda za upravljanje lika
	public void key(KeyEvent e) 
	{
		
		
//		if(e.getKeyCode() == 32){ // Key :: SPACE
//			dead = false;
//			x_speed = 0;
//			y_speed = 0;
//		}
		
	}

	@Override
	public void keyPressed(KeyEvent e) {
		if(player){ // Ce je player 1 ima smerne tipke
			if(e.getKeyCode() == 38 && y_speed != height){ // Key :: UP
				y_speed = -height;
				x_speed = 0;
			}
			if(e.getKeyCode() == 40 && y_speed != -height){ // Key :: DOWN
				y_speed = +height;
				x_speed = 0;
			}
			if(e.getKeyCode() == 37 && x_speed != width){ // Key :: LEFT
				x_speed = -width;
				y_speed = 0;
			}
			if(e.getKeyCode() == 39 && x_speed != -width){ // Key :: RIGHT
				x_speed = +width;
				y_speed = 0;
			}
		}else{ // Ce pa player 2 pa WSAD
			if(e.getKeyCode() == 87 && y_speed != height){ // Key :: W
				y_speed = -height;
				x_speed = 0;
			}
			if(e.getKeyCode() == 83 && y_speed != -height){ // Key :: S
				y_speed = +height;
				x_speed = 0;
			}
			if(e.getKeyCode() == 65 && x_speed != width){ // Key :: A
				x_speed = -width;
				y_speed = 0;
			}
			if(e.getKeyCode() == 68 && x_speed != -width){ // Key :: D
				x_speed = +width;
				y_speed = 0;
			}
		}
		
	}

	@Override
	public void keyReleased(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void keyTyped(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}

}

